import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';
import 'package:gold/route/routes.dart';
import 'package:gold/route/xfg_nav.dart';
import 'package:gold/utils/res.dart';
import 'package:gold/utils/xfg_color.dart';
import 'package:gold/utils/xfg_size.dart';
import 'package:gold/widgets/xfg_text.dart';

/// 描述: 首页底部导航栏
///
/// @Author tangzhentao
///  @CreatedTime 10:11 2022/6/18
///

class BottomBar extends StatelessWidget {
  final PageController pageController;

  const BottomBar({super.key, required this.pageController});

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider.value(
      value: pageController,
      child: Selector<PageController, double?>(
        selector: (_, k) => k.page,
        builder: (_, page, __) => Container(
          height: XfgSize().size67 + XfgSize().bottomBarHeight,
          padding: EdgeInsets.only(bottom: ScreenUtil().bottomBarHeight),
          decoration: BoxDecoration(
            color: XfgColor.white,
            boxShadow: [BoxShadow(color: XfgColor.text1D.withValues(alpha: 0.18), offset: Offset(0, 1.w), blurRadius: XfgSize().size6)]
          ),
          child: Row(
            children: [
              _itemBottomBar(Bottom('首页', Res.imagesHome, Res.imagesHomeSelected), 0, 0 == page || page == null),
              _itemBottomBar(Bottom('商品', Res.imagesGoods, Res.imagesGoodsSelected), 1, 1 == page),
              _itemBottomBar(Bottom('积分商城', Res.imagesIntegrationMall, Res.imagesIntegrationMallSelected), 2, 2 == page),
              _itemBottomBar(Bottom('我的', Res.imagesMy, Res.imagesMySelected), 3, 3 == page),
            ],
          ),
        ),
      ),
    );
  }

  Widget _itemBottomBar(Bottom bottom, int index, bool select) => Expanded(
    child: GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () {
        if (index == 2) {
          XfgNav.pushNamed(Routes.integrationMall);
          return;
        }

        if (!select) {
          pageController.jumpToPage(index);
        }
      },
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        mainAxisSize: MainAxisSize.max,
        children: [
          SizedBox(height: XfgSize().size8_5),
          Image.asset(select ? bottom.imageSelect:bottom.image, width: XfgSize().size30, height: XfgSize().size30),
          SizedBox(height: XfgSize().size3),
          XfgText(bottom.tab, fontSize: XfgSize().text14, color: select ? XfgColor.theme:XfgColor.text1)
        ],
      ),
    ),
  );
}

class Bottom {
  final String tab;
  final String image;
  final String imageSelect;

  Bottom(this.tab, this.image, this.imageSelect);
}
